// package/pages/invoice/invoice.js
let myRequest = require('../../../common/script/myRequest.js');
let api = require('../../../common/script/api.js');
let login = require('../../../common/script/login');
let wxbarcode = require('../../../utils/wxbarcode/index.js')
const utils = require('../../../utils/util.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isEnd: true,
    isWithout: true,
    page: 1, // 页码
    pageSize: 10,
    listData: [],
  },

  getData() {
    const that = this;
    let data = {
      token: wx.getStorageSync('token'),
      page: that.data.page,
      page_size: that.data.pageSize,
    }
    wx.showLoading({
      title: '加载中...',
    })
    myRequest.EasyHttp.post(api.reqUrls.invoiceList, data).then(res => {
      wx.hideLoading();
      wx.stopPullDownRefresh();
      wx.hideNavigationBarLoading();
      if (res.data.status == 1) {
        if (res.data.result.length == 0) {
          that.setData({
            isWithout: false
          })
        } else {
          that.setData({
            listData: res.data.result
          })
        }
      }
    })
  },
  // 详情
  toInvoiceDetail(e) {
    const that = this;
    let data = that.data.listData[e.currentTarget.dataset.index]
    wx.navigateTo({
      url: `/package/pages/invoiceDetail/invoiceDetail?orgname=${data.orgname}&province_type=${data.provinde_type}&date=${data.date}&name=${data.name}&code=${data.code}&address=${data.address}&phone=${data.phone}&bank=${data.bank}&account=${data.account}&email=${data.email}&mobilephone=${data.mobilephone}`,
    })
  },
  // 添加
  scanCode() {
    const that = this;
    wx.showLoading({
      title: '加载中...',
    })
    wx.scanCode({
      success: (res) => {
        console.log(res, 7979797)
        wx.hideLoading();
        const url = res.result.substring(res.result.indexOf('?') + 1)
        const params = url.split('&')
          .reduce(function (acc, param) {
            const [key, value] = param.split('=');
            acc[key] = decodeURIComponent(value);
            return acc;
          }, {});
         
        let province_type = params['m'] ? 1 : 2;
        let orgcode = params['o'];
        let key = encodeURIComponent(params['k']);
        let date = params['d'];
        wx.navigateTo({
          url: `/page/detail/pages/addLnvoice/addLnvoice?province_type=${province_type}&orgcode=${orgcode}&key=${key}&date=${date}`,
        })
      },
      fail: (err) => {
        wx.hideLoading();
        // wx.showToast({
        //   title: '扫描失败请重试',
        //   icon: 'error',
        //   duration: 2000
        // })
        console.error(err);
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const that = this;
    login.checkLogin().then(res => {
      that.getData()
    }).catch(err => {
      app.appCallBack = (e) => {
        that.getData()
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    const that = this;
    wx.showNavigationBarLoading()
    that.setData({
      page: 1,
      isEnd: true,
      isWithout: true,
    })
    that.getData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    const that = this;
    if (!that.data.isEnd) {
      return
    }
    let data = {
      token: wx.getStorageSync('token'),
      page: that.data.page + 1,
      page_size: that.data.pageSize,
    }
    wx.showLoading({
      title: '加载中...',
    })
    myRequest.EasyHttp.post(api.reqUrls.invoiceList, data).then(res => {
      wx.hideLoading();
      if (res.data.status == 1) {
        if (res.data.result.length == 0) {
          wx.showToast({
            title: '没有更多了',
            icon: 'none',
            duration: 1000
          })
          that.setData({
            isEnd: false
          })
        } else {
          that.setData({
            listData: [...that.data.listData, ...res.data.result],
            page: that.data.page + 1
          })
        }
      }
    })
  },
  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage() {

  // }
})